<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房屋编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择缴费开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择缴费结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="缴费状态" prop="costSts">
        <el-select v-model="queryParams.costSts" placeholder="请选择缴费状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.COST001"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="缴费渠道" prop="costWay">
        <el-select v-model="queryParams.costWay" placeholder="请选择缴费渠道" clearable size="small">
          <el-option
            v-for="dict in dict.type.COST002"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="对账状态" prop="checkSts">
        <el-select v-model="queryParams.checkSts" placeholder="请选择对账状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.COST003"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发票号码" prop="billNo">
        <el-input
          v-model="queryParams.billNo"
          placeholder="请输入发票号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleApply"
        >申请发票</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handlePrint"
        >打印发票</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="print-wrap" ref="print" hidden>
      <div class="print-preview" :style="{ width: '320px' }">
        <div class="title">打印票据</div>
        <div class="divide_line"></div>
        <!-- ...小票内容html -->
        <div class="tip">请保存好小票，不要丢弃！</div>
        <!-- 底下logo二进制文件流，一定要用二进制文件流，保证浏览器打印能够拿到log -->
        <!-- <img class="image-wrap" src="data:image/png;base64,二进制文件流">-->
        <div class="div1" id="year">ddd</div>
        <div class="div2" id="month"></div>
        <div class="div3" id="date"></div>
        <div class="div5" id="userName"></div>
        <div class="div6" id="space"></div>
        <div class="div7" id="addr"></div>
        <div class="div8" id="tel"></div>
        <div class="div10" id="formula"></div>
        <div class="div11" id="moneyTemp"></div>
        <div class="div12" id="money"></div>
        <div class="div13" id="unit"></div>
      </div>
    </div>

    <el-table :cell-style="tableRowClassName" v-loading="loading" :data="costQueryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="小区" align="center" prop="areaName" />
      <el-table-column label="楼宇" align="center" prop="groupName" />
      <el-table-column label="单元" align="center" prop="unitName" />
      <el-table-column label="户" align="center" prop="doorName" />
      <el-table-column label="房屋编号" align="center" prop="houseId" />
      <el-table-column label="流水号" align="center" prop="serial" />
      <el-table-column label="缴费金额" align="center" prop="costMoney" />
      <el-table-column label="缴费时间" align="center" prop="costDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.costDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费状态" align="center" prop="costSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.COST001" :value="scope.row.costSts"/>
        </template>
      </el-table-column>
      <el-table-column label="缴费渠道" align="center" prop="costWay">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.COST002" :value="scope.row.costWay"/>
        </template>
      </el-table-column>
      <el-table-column label="对账状态" align="center" prop="checkSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.COST003" :value="scope.row.checkSts"/>
        </template>
      </el-table-column>
      <el-table-column label="发票号码" align="center" prop="billNo" />
      <el-table-column label="收费员" align="center" prop="creater" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listCostQuery, getCostQuery, delCostQuery, addCostQuery, updateCostQuery,apply,print, getPrint } from "@/api/fund/costQuery";
export default {
  name: "CostQuery",
  dicts: ['COST003', 'COST002', 'COST001'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 缴费信息表格数据
      costQueryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        startDate: null,
        endDate: null,
        costSts: null,
        costWay: null,
        checkSts: null,
        billNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({row}){
      return row.costSts=='1'?'color: black;':(row.costSts=='2'?'color: red;':"")
    },
    /** 查询缴费信息列表 */
    getList() {
      this.loading = true;
      listCostQuery(this.queryParams).then(response => {
        this.costQueryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        costId: null,
        houseId: null,
        serial: null,
        costMoney: null,
        costDate: null,
        costSts: null,
        costWay: null,
        checkSts: null,
        billNo: null,
        creater: null,
        createDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.costId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加缴费信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const costId = row.costId || this.ids
      getCostQuery(costId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改缴费信息";
      });
    },

    handleApply(row){
      const costId = row.costId || this.ids
      this.$modal.confirm('是否确认申请发票信息编号为"' + costId + '"的数据项？').then(function() {
        return apply(costId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申请成功");
      }).catch(() => {});
    },

    handlePrint(row) {
      const costId = row.costId || this.ids
      print(costId).then(response => {
        debugger
        let baseUrl =response.data.pictureUrl;
        window.open(baseUrl);
      });
    },
    onPrint(row) {
      this.reset();
      const costId = row.costId || this.ids
      getPrint(costId).then(response => {
        const printNode = this.$refs.print
          if(!printNode) return
          const newIframe = document.createElement('iframe');
          newIframe.setAttribute('style', 'width:0px;height:0px;position:absolute;left:-9999px;top:-9999px;');
          newIframe.setAttribute('align', 'center');
          document.body.appendChild(newIframe);
          document.getElementById("year").innerHTML=response.data.year;
          document.getElementById("money").innerHTML=response.data.costMoney;
          document.getElementById("date").innerHTML=response.data.costDate;
          document.getElementById("userName").innerHTML=response.data.userName;
          document.getElementById("space").innerHTML=response.data.space;
          document.getElementById("addr").innerHTML=response.data.addr;
          document.getElementById("tel").innerHTML=response.data.tel;
          document.getElementById("formula").innerHTML=response.data.formula;
          document.getElementById("moneyTemp").innerHTML=response.data.moneyTemp;
          document.getElementById("month").innerHTML=response.data.month;
          document.getElementById("unit").innerHTML="房产管理局物业管理办公室";
          let doc = null;
          doc = newIframe.contentWindow.document;
          doc.write(`
            <style type="text/css">
              @media print {
                @page {
                  size: auto;
                  margin: 1mm;
                  table { page-break-after: auto; }
                  tr    { page-break-inside: avoid; page-break-after: auto; }
                  td    { page-break-inside: avoid; page-break-after: auto; }
                  thead { display: table-header-group; }
                  tfoot { display: table-footer-group; }
                }
              }
              .print-preview { display: flex; flex-direction: column; color: rgba(51, 51, 51, 1); font-family: Microsoft YaHei, '黑体', STSong, '宋体',Consolas, Monaco, Droid, Sans, Mono, Source, Code, Pro, Menlo, Lucida, Sans, Type, Writer, Ubuntu, Mono; }
              .title { display: flex; justify-content: center; font-size: 21px; height: 50px; line-height: 50px; font-weight: bold; }
              .divide_line { justify-content: center; border-bottom: 1px dashed rgba(51, 51, 51, 1); }
              body{
                          margin:0;
                      }
                      .main {
                          width: 1470px;
              		    height: 622px;
              		    border: 0px #000 solid;
                          color: #000;
                          font-weight:bold;
                          line-height: 13px;
                          font-size: 28px;
                          position: relative;
                      }
                      .main div{
                          z-index: 11;
                          position: absolute;
                          /*background:#0f0;*/
                          height: 13px;
                      }
                      .img1{
                          width: 100%;
                          position: absolute;
                          top:0;
                          z-index: 1;
                          display: none;
                      }
                      .div1{
                          top: 200px;
                          left: 300px;
                          width: 100px;
                      }
                      .div2{
                          top: 83px;
                          left: 300px;
                          width: 100px;
                      }
                      .div3{
                          top: 83px;
                          left: 390px;
                          width: 100px;
                      }

                      .div5{
                          top: 140px;
                          left: 300px;
                          width: 300px;
                      }
                      .div6{
                          top: 140px;
                          left: 855px;
                          width: 400px;
                      }
                      .div7{
                          top: 224px;
                          left: 300px;
                          width: 500px;
                      }
                      .div8{
                          top: 224px;
                          left: 885px;
                          width: 300px;
                      }

                      .div10{
                          top: 300px;
                          left: 300px;
                          width: 600px;
                      }
              		.div11{
                          top: 440px;
                          left: 300px;
                          width: 330px;
                      }
              		.div12{
              			top: 447px;
              			left: 920px;
              			width: 300px;
              		}
              		.div13{
              			top: 500px;
              			left: 300px;
              			width: 300px;
              		}

              		.div14{
              			top: 500px;
              			left: 643px;
              			width: 300px;
              		}
            </style>
            <div style="width:100%;height:auto;width:320px;margin:0px auto;"align="center">
              ${printNode.innerHTML}
            </div>`)
        doc.close();
        setTimeout(() => {
          newIframe.contentWindow.focus();
          newIframe.contentWindow.print();
          document.body.removeChild(newIframe);
        }, 10);
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.costId != null) {
            updateCostQuery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCostQuery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const costIds = row.costId || this.ids;
      this.$modal.confirm('是否确认删除缴费信息编号为"' + costIds + '"的数据项？').then(function() {
        return delCostQuery(costIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fund/costQuery/export', {
        ...this.queryParams
      }, `costQuery_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>

    </style>

